#include "stdafx.h"
#include "CheckBoxExample.h"

#include "hwinapplication.h"
#include "hwinform.h"
#include "hwinmenu.h"
#include "hwinstdctrls.h"
#include <iostream>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using namespace harlinn::windows;

class CheckBoxForm : public Form
{
    std::shared_ptr<CheckBox> checkBox;
public:
    typedef Form Base;
    CheckBoxForm();
protected:
    virtual void DoOnInitialize();
    virtual void DoOnSize(Message& message);
    virtual void DoOnPaint(Message& message);
};

CheckBoxForm::CheckBoxForm()
    : Base()
{
    SetText(L"CheckBox example");
    SetSize(300,70);
}

void CheckBoxForm::DoOnInitialize()
{
    Base::DoOnInitialize(); 
    auto self = As<CheckBoxForm>();
    checkBox = make_control<CheckBox>(self);
    checkBox->SetText(L"Click me!");
    checkBox->OnClick.connect([&](Control* sender)
    {   
        if(checkBox->Checked())
        {
            SetText(L"CheckBox Checked"); 
        }
        else
        {
            SetText(L"CheckBox Not Checked"); 
        }
    });
}

void CheckBoxForm::DoOnSize(Message& message)
{
    harlinn::windows::Rectangle clientRect = GetClientRect();
    checkBox->MoveWindow(5,5,clientRect.Width() - 10,21);
}

void CheckBoxForm::DoOnPaint(Message& message)
{
    Base::DoOnPaint(message);
    auto dc = std::make_shared<PaintDeviceContextHandle>(As<Control>());
    dc->DrawDesktopWallPaper();
}

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
    try
    {
        auto application = make_component<Application>();
        auto form = make_control<CheckBoxForm>();
        
        auto result = application->Run(form);

        return result;
    }
    catch(std::exception& exc)
    {
        std::cout << exc.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Unknown exception" << std::endl;    
    }
    return 0;
}